import axios from "axios";
import { Message } from "element-ui";
// import store from '@/store';
import { statusMap } from "./statusMap";
import qs from "qs";

const GW = "http://218.58.138.126:8081";
const NUM = "http://218.58.138.126:8081";
const OLD = "http://218.58.138.126:8081";
const CUR = "http://218.58.138.126:8081";
// create an axios instance
const service = axios.create({
  baseURL: "", // url = base url + request url
  // withCredentials: true, // send cookies when cross-domain requests
  timeout: 5000, // request timeout
});

// request interceptor
service.interceptors.request.use(
  (config) => {
    if (process.env.NODE_ENV !== "development") {
      if (config.url.includes("/gw")) {
        config.url = config.url.replace("/gw", GW);
      } else if (config.url.includes("/num")) {
        config.url = config.url.replace("/num", NUM);
      } else if (config.url.includes("/old")) {
        config.url = config.url.replace("/old", OLD);
      } else if(config.url.includes("/cur")) {
        config.url = config.url.replace("/cur", CUR);
      }
    }
    return config;
  },
  (error) => {
    // do something with request error
    console.log(error); // for debug
    return Promise.reject(error);
  }
);

// response interceptor
service.interceptors.response.use(
  /**
   * If you want to get http information such as headers or status
   * Please return  response => response
   */

  /**
   * Determine the request status by custom code
   * Here is just an example
   * You can also judge the status by HTTP Status Code
   */
  (response) => {
    const res = response.data;
    return res;
  },
  (error) => {
    if (error === undefined || error.code === "ECONNABORTED") {
      // Message({
      //   message: "服务请求超时",
      //   type: "warning",
      //   duration: 5 * 1000,
      // });
      return Promise.reject(error);
    }

    const { response } = error;
    if (response.status === 401 && window.microApp) {
      // 通知micro-app主应用跳转到登录页面
      console.log("child redirect");
      window.microApp.dispatch({ path: "login" });
    }

    if (response.status === 500) {
      const message = response.data.msg || response.data.message;

      // Message({
      //   message,
      //   type: "error",
      //   duration: 5 * 1000,
      // });

      return;
    }

    const text =
      response.data.msg ||
      response.data.message ||
      statusMap[response.status] ||
      response.statusText ||
      "服务器发生错误";

    Message({
      message: response.status + text,
      type: "error",
      duration: 5 * 1000,
    });

    return Promise.reject(error);
  }
);

/**
 * @param {String} url
 * @param {Object} data
 * @returns Promise
 */
const post = function (url, data, headers) {
  return new Promise((resolve, reject) => {
    service
      .post(url, data, headers)
      .then((res) => {
        resolve(res);
      })
      .catch((err) => {
        reject(err);
      });
  });
};
const get = function (url, params) {
  return new Promise((resolve, reject) => {
    if (params) {
      params = qs.stringify(params);
    }
    service
      .get(url + (params ? "?" + params : ""))
      .then((res) => {
        resolve(res);
      })
      .catch((err) => {
        reject(err);
      });
  });
};
const put = function (url, data) {
  return new Promise((resolve, reject) => {
    service
      .put(url, data)
      .then((res) => {
        resolve(res);
      })
      .catch((err) => {
        reject(err);
      });
  });
};

const deleteRequest = function (url, data) {
  return new Promise((resolve, reject) => {
    service
      .delete(url, data)
      .then((res) => {
        resolve(res);
      })
      .catch((err) => {
        reject(err);
      });
  });
};

const patch = function (url, data) {
  // return new Promise((resolve, reject) => {
  //   service
  //     .patch(url, data)
  //     .then((res) => {
  //       resolve(res);
  //     })
  //     .catch((err) => {
  //       reject(err);
  //     });
  // });
  return service.patch(url, data);
};

const uploadUrl = process.env.VUE_APP_UPLOAD_URL;

export { post, get, put, deleteRequest, patch, uploadUrl };
